package com.ruoyi.web.controller.system;

import java.util.List;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.constant.UserConstants;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.framework.shiro.service.SysPasswordService;
import com.ruoyi.framework.util.ShiroUtils;
import com.ruoyi.system.domain.AdminClass;
import com.ruoyi.system.domain.SysUser;
import com.ruoyi.system.service.IAdminClassService;
import com.ruoyi.system.service.ISysUserService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * 教师Controller
 *
 * @author ruoyi
 * @date 2020-06-09
 */
@Controller
@RequestMapping("/system/teacher")
public class AdminTeacherController extends BaseController {

  private String prefix = "system/teacher";

  @Autowired
  private ISysUserService userService;
  @Autowired
  private SysPasswordService passwordService;
  @Autowired
  private IAdminClassService adminClassService;

  @RequiresPermissions("system:teacher:view")
  @GetMapping()
  public String teacher () {
    return prefix + "/teacher";
  }

  /**
   * 查询教师列表
   */
  @RequiresPermissions("system:teacher:list")
  @PostMapping("/list")
  @ResponseBody
  public TableDataInfo list (SysUser user) {
    startPage();
    user.setUserType(UserConstants.TEACHER_USER_TYPE);
    List<SysUser> list = userService.selectUserList(user);
    return getDataTable(list);
  }


  /**
   * 新增教师
   */
  @GetMapping("/add")
  public String add (ModelMap mmap) {
    AdminClass adminClass= new AdminClass();
    adminClass.setStatus("0");
    mmap.put("classList", adminClassService.selectAdminClassList(adminClass));
    return prefix + "/add";
  }

  /**
   * 新增保存教师
   */
  @RequiresPermissions("system:teacher:add")
  @Log(title = "教师", businessType = BusinessType.INSERT)
  @PostMapping("/add")
  @ResponseBody
  public AjaxResult addSave (SysUser sysUser) {
    if (UserConstants.USER_NAME_NOT_UNIQUE.equals(userService.checkLoginNameUnique(sysUser.getLoginName())))
    {
      return error("新增用户'" + sysUser.getLoginName() + "'失败，登录账号已存在");
    }
    else if (UserConstants.USER_EMAIL_NOT_UNIQUE.equals(userService.checkEmailUnique(sysUser)))
    {
      return error("新增用户'" + sysUser.getLoginName() + "'失败，邮箱账号已存在");
    }
    sysUser.setSalt(ShiroUtils.randomSalt());
    sysUser.setPassword(passwordService.encryptPassword(sysUser.getLoginName(), sysUser.getPassword(), sysUser.getSalt()));
    sysUser.setCreateBy(ShiroUtils.getLoginName());
//    if(1!=ShiroUtils.getCompanyId()){
      sysUser.setCompanyId(ShiroUtils.getCompanyId());
      sysUser.setCompanyName(ShiroUtils.getCompanyName());
//    }
    sysUser.setUserType(UserConstants.TEACHER_USER_TYPE);
    return toAjax(userService.insertUser(sysUser));
  }

  /**
   * 修改教师
   */
  @GetMapping("/edit/{userId}")
  public String edit (@PathVariable("userId") Long userId, ModelMap mmap) {
    mmap.put("classList", adminClassService.selectClassByUserId(userId));
    mmap.put("user", userService.selectUserById(userId));
    return prefix + "/edit";
  }

  /**
   * 修改保存教师
   */
  @RequiresPermissions("system:teacher:edit")
  @Log(title = "教师", businessType = BusinessType.UPDATE)
  @PostMapping("/edit")
  @ResponseBody
  public AjaxResult editSave (SysUser user) {
    userService.checkUserAllowed(user);
    if (UserConstants.USER_PHONE_NOT_UNIQUE.equals(userService.checkPhoneUnique(user)))
    {
      return error("修改用户'" + user.getLoginName() + "'失败，手机号码已存在");
    }
    else if (UserConstants.USER_EMAIL_NOT_UNIQUE.equals(userService.checkEmailUnique(user)))
    {
      return error("修改用户'" + user.getLoginName() + "'失败，邮箱账号已存在");
    }
    if(StringUtils.isNotEmpty(user.getPassword())){
      user.setPassword(passwordService.encryptPassword(user.getLoginName(), user.getPassword(), user.getSalt()));
    }
    user.setUpdateBy(ShiroUtils.getLoginName());
    return toAjax(userService.updateUser(user));
  }

  /**
   * 删除教师
   */
  @RequiresPermissions("system:teacher:remove")
  @Log(title = "教师", businessType = BusinessType.DELETE)
  @PostMapping("/remove")
  @ResponseBody
  public AjaxResult remove (String ids) {
    try
    {
      return toAjax(userService.deleteUserByIds(ids));
    }
    catch (Exception e)
    {
      return error(e.getMessage());
    }
  }

  /**
   * 老师状态修改
   */
  @Log(title = "教师管理", businessType = BusinessType.UPDATE)
  @RequiresPermissions("system:teacher:edit")
  @PostMapping("/changeStatus")
  @ResponseBody
  public AjaxResult changeStatus (SysUser sysUser) {
    return toAjax(userService.changeStatus(sysUser));
  }
}
